//
//  ByGoodsListVC.m
//  mijifen_ios
//
//  Created by mc on 2017/11/6.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByGoodsListVC.h"

// cell
#import "ByCellGoodsList.h"

// vc
#import "ByGoodsIndexRecommendVC.h"

@interface ByGoodsListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ByGoodsListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品列表";
    [self addLeftBackBarButtonItem];
    [self initTable];
    [self fetchData];
}

#pragma mark - 获取数据
- (void)fetchData{
    for (int i = 0; i < 10; i ++) {
        ByModelGoodsList *model = [[ByModelGoodsList alloc] init];
        model.describe = @"耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克";
        model.price = @"888787868.09";
        model.stock = @"200";
        model.sold = @"100";
        model.status = @"1";
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
}

#pragma mark - getter
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark - 创建视图
- (void)initTable{
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom);
        }
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellGoodsList *cell = [ByCellGoodsList cellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.section];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    ByModelGoodsList *model = self.dataArray[section];
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    footerView.backgroundColor = [UIColor whiteColor];
    NSArray *buttonAry;
    switch (model.status.integerValue) {
        case 1:
            buttonAry = @[@"取消推荐",@"删除商品",@"上架"];
            break;
        case 0:
            buttonAry = @[@"推荐",@"删除商品",@"下架"];
            break;
        default:
            break;
    }
    CGFloat x,y,width;
    y = 10;
    width = [@"删除商品" getSizeOfString:16].width + 20;
    for (int i = 0; i < buttonAry.count; i ++) {
        CGFloat margin = 15;
        x = SCREEN_WIDTH - (width + margin) * (i+1) ;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, 30)];
        [button setTitle:buttonAry[i] forState:UIControlStateNormal];
        [footerView addSubview:button];
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 4.0;
        button.titleLabel.font = FONT(14);
        button.tag = section + 10;
        if ([buttonAry[i] isEqualToString:@"推荐"]) {
            [button setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
            button.layer.borderColor = BY_TEXT_COLOR.CGColor;
        }else{
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.layer.borderColor = [UIColor colorWithWhite:0.95 alpha:1].CGColor;
        }
        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

#pragma mark - 点击事件
- (void)clickAction:(UIButton *)button{
//    if ([button.titleLabel.text isEqualToString:@"推荐"]) {
        ByGoodsIndexRecommendVC *vc = [[ByGoodsIndexRecommendVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
